<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人员管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: "HarmonyOS Sans SC", "Microsoft YaHei", sans-serif;
            background-color: #f5f5f5;
            color: #252b3a;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .header {
            background-color: #1c2b36;
            color: white;
            padding: 20px 0;
            text-align: center;
        }

        .header h1 {
            font-size: 24px;
            font-weight: normal;
        }

        .login-container {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .login-box {
            background-color: white;
            padding: 30px;
            border-radius: 4px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        .login-box h2 {
            font-size: 20px;
            margin-bottom: 20px;
            color: #1c2b36;
            text-align: center;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #575d6c;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #dfe1e6;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.2s;
        }

        .form-group input:focus {
            outline: none;
            border-color: #1c2b36;
        }

        .login-button {
            width: 100%;
            padding: 12px;
            background-color: #1c2b36;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.2s;
        }

        .login-button:hover {
            background-color: #2d3e4a;
        }

        .error-message {
            color: #e64552;
            font-size: 14px;
            margin-top: 15px;
            text-align: center;
            display: none;
        }

        .footer {
            text-align: center;
            padding: 20px 0;
            color: #575d6c;
            font-size: 14px;
            background-color: white;
        }

        @media (max-width: 480px) {
            .login-box {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <h1>人员管理系统</h1>
    </header>

    <main class="login-container">
        <div class="login-box">
            <h2>用户登录</h2>
            <form id="loginForm" onsubmit="return handleLogin(event)">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" required>
                </div>
                <button type="submit" class="login-button">登录</button>
                <div id="errorMessage" class="error-message"></div>
            </form>
        </div>
    </main>

    <footer class="footer">
        <p>© 2024 人员管理系统 版权所有</p>
    </footer>

    <script>
        async function handleLogin(event) {
            event.preventDefault();
            const errorMessage = document.getElementById('errorMessage');
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            try {
                const formData = new FormData();
                formData.append('username', username);
                formData.append('password', password);

                const response = await fetch('/api/auth/login', {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    const data = await response.json();
                    // 保存认证信息到 sessionStorage
                    sessionStorage.setItem('token', data.access_token);
                    sessionStorage.setItem('username', data.username);
                    sessionStorage.setItem('roleId', data.role_id.toString());
                    // 跳转到 docs 页面
                    window.location.href = '/docs';
                } else {
                    const error = await response.json();
                    errorMessage.textContent = error.detail || '登录失败，请检查用户名和密码';
                    errorMessage.style.display = 'block';
                }
            } catch (error) {
                errorMessage.textContent = '登录失败，请稍后重试';
                errorMessage.style.display = 'block';
            }
        }
    </script>
</body>
</html> 